package com.zxbit.uangstore.presenter;

import com.zxbit.uangstore.BuildConfig;
import com.zxbit.uangstore.base.BasePresenter;
import com.zxbit.uangstore.business.CustomConsumer;
import com.zxbit.uangstore.cache.UserInfo;
import com.zxbit.uangstore.constant.Constant;
import com.zxbit.uangstore.constant.ParamsFactory;
import com.zxbit.uangstore.domain.BlankResponse;
import com.zxbit.uangstore.domain.ProductItemData;
import com.zxbit.uangstore.domain.ProductResponse;
import com.zxbit.uangstore.domain.UpdateResponse;
import com.zxbit.uangstore.mvpview.MainMvpView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

public class MainPresenter extends BasePresenter {
    private MainMvpView mvpView;

    public MainPresenter(MainMvpView mvpView) {
        this.mvpView = mvpView;
    }

    public void getProduct(int index, int currentPage, String prodAlias) {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("limit", Constant.LIMIT); // 每页10条
        paramsMap.put("page", String.valueOf(currentPage));
        paramsMap.put("prodAlias", prodAlias);

        Observable<ProductResponse> ob = null;
        if (index == 0) {
            ob = req.getCashLoan(ParamsFactory.getCommonParams(paramsMap));
        } else if (index == 1) {
            ob = req.getInstallment(ParamsFactory.getCommonParams(paramsMap));
        }
        if (ob == null) return;

        toSubScribe(ob, new CustomConsumer<ProductResponse>() {
            @Override
            protected void processCommonData(ProductResponse productResponse) {
                mvpView.onGetDataNext(productResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    public void insertHistory(final ProductItemData data, final boolean isInstall) {
        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("productName", data.getProductName());
        paramsMap.put("score", data.getScore());
        paramsMap.put("logoUrl", data.getLogoUrl());
        paramsMap.put("isboutique", data.getIsboutique());
        paramsMap.put("productVersion", data.getProductVersion());
        paramsMap.put("downloadUrl", data.getDownloadUrl());
        paramsMap.put("productId", data.getId());
        paramsMap.put("type", data.getType());

        toSubScribe(req.insertHistory(UserInfo.getToken(), ParamsFactory.getCommonParams(paramsMap)),
                new CustomConsumer<BlankResponse>() {
                    @Override
                    protected void processCommonData(BlankResponse blankResponse) {
                        mvpView.onGetDataNext(blankResponse, isInstall, data);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mvpView.onGetDataError(throwable);
                    }
                });
    }

    public void googlePlayUpdate() {
        toSubScribe(req.getGooglePlayUpdateInfo(UserInfo.getToken(), ParamsFactory.getCommonParams(null)), new CustomConsumer<UpdateResponse>() {
            @Override
            protected void processCommonData(UpdateResponse updateResponse) {
                mvpView.onGetDataNext(updateResponse);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }
}
